#include <avr/io.h>


#include "usart.h"

void USART_init(unsigned int baudrate)
{
    
    /* set baud rate */
    _UBRRH_UART0 = (uint8_t)(UART_BAUD_SELECT(2400, F_CPU) >> 8);  /* high byte */
    _UBRRL_UART0 = (uint8_t)( UART_BAUD_SELECT(2400, F_CPU) );         /* low byte */
    
    /* set mode */
    _UCSRC_UART0 = UART_UCSRC;

    /* enable transmitter, receiver */
    _UCSRB_UART0 = _BV(_TXEN_UART0) | _BV(_RXEN_UART0);

}



void USART_sendchar(char data)
{
        /* loop until data has been transmitted */
    while (!(_UCSRA_UART0 & _BV(_UDRE_UART0)));

    /* put data in buffer */
    _UDR_UART0 = data;

}

char USART_recvchar(void)
{
     /* wait if a byte has been received */
    while (!(_UCSRA_UART0 & _BV(_RXC_UART0)));

    /* return received byte */
    return _UDR_UART0;
}

char* USART_recv(void)
{
	char temp;
	unsigned char counter = 0;
	
	while(temp != '\n' || counter > len(usartbuffer)-1){
		temp = USART_recvchar();
		usartbuffer[counter] = temp;
		counter++;
	}
	
	usartbuffer[counter]= '\0';
	return usartbuffer;
	
}


void uart_send(char* data){
    while (*data) 
      USART_sendchar(*data++);

}




